<?php

namespace App\Http\Controllers\UserApi;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Http\Requests\UserApi\SubscribeRequest;
use App\Models\UserSubscribe;
use App\Services\Api\SubscribeServices;
use App\Models\ClassBase;
use App\Models\Classroom;
use App\Models\ClassBaseInfo;


class UserSubscribeController extends BaseController
{

    public function create_subscribe(SubscribeRequest $subscribeRequest,
                                     UserSubscribe $subscribeModel,
                                     //SubscribeServices $subscribeServices,
                                     ClassBase $classBase,
                                     ClassBaseInfo $baseInfo)
    {
        $input = $subscribeRequest->post();
        $classBase = $baseInfo->with([
            'class_base'=>function($q){
                $q->select(['id','class_name']);
            },
            'teacher'=>function($q){
                $q->select(['id','teacher_name']);
            },
            'classsroom'=>function($q){
                $q->select(['id','numbering']);
            },
            'store'=>function($q){
                $q->select(['id','store_name']);
            },
        ])
            ->find($input['class_base_info_id']);
        $time = explode('~',$classBase->class_time);
        if(count($time)>0){
            //$subscribe_time = $input['subscribe_time'] / 1000;
            $subscribe_time = $input['subscribe_time'];
            $start_time = $subscribe_time + HisToS($time[0]);
            $end_time = $subscribe_time + HisToS($time[1]);
        }

        $input['subscribe_status'] = 2;
        $classroom_number = $subscribeModel->ClassUseNumber($classBase->classsroom->id,$start_time,$end_time);
        if($classroom_number >= $classBase->class_number_people){
            //人數已滿 排隊
            $input['subscribe_status'] = 1;
        }

        $surplus_integral = auth('api') -> user()->total_integral - $classBase->buy_integral;
        //验证积分
        if($surplus_integral < 0){
            return $this->fail('积分不足');
        }

        //計算6天內時間 有沒有預約過
        $seven_date = date("Y-m-d H:i:s",$subscribe_time + strtotime("+6 day"));
        $check_base_info_number = $subscribeModel->CheckSevenClassInfoNumber($input['class_base_info_id'],date("Y-m-d H:i:s",$start_time),$seven_date,auth('api') -> user()->id);
        //dd($check_base_info_number,$classroom_number,$classBase);
        if($check_base_info_number > 0){
            return $this->fail('請勿重複預約');
        }

        $input['user_id'] = auth('api') -> user()->id;
        $input['class_base_id'] = $classBase->class_base_id;
        $input['store_id'] = $classBase->store_id;
        $input['class_name'] = $classBase->class_base->class_name;
        $input['store_name'] = $classBase->store->store_name;
        $input['class_classroom_id'] = $classBase->classsroom->id;
        $input['class_classroom_name'] = $classBase->classsroom->numbering;
        $input['teacher_id'] = $classBase->teacher->id;
        $input['teacher_name'] = $classBase->teacher->teacher_name;
        $input['use_integral'] = $surplus_integral;
        $input['class_time'] = date("Y-m-d H:i:s",$start_time);
        $input['end_time'] = date("Y-m-d H:i:s",$end_time);
        $input['created_at'] = date("Y-m-d H:i:s");
        //快照數據
        $input['class_info'] = json_encode($classBase);

        $res = $subscribeModel->create($input);
        if($res){
            return $this->success();
        }

        return $this->fail('保存失敗');
    }

    public function my_subscribe(Request $request,UserSubscribe $subscribeModel,SubscribeServices $subscribeServices){
        //$where = $request->input('order');
        $data = $subscribeServices->my_subscribe($request,auth('api') -> user()->id,$subscribeModel);
        return $this->success($data);
    }

    public function change_subscribe(Request $request,UserSubscribe $subscribeModel,SubscribeServices $subscribeServices){
        $check_change = $subscribeServices->check_change_subscribe($request->subscribe_id);
        if(!$check_change){
            return $this->fail('超时不能取消');
        }
        $res = $subscribeServices->change_subscribe($request,$subscribeModel);
        if($res){
            $this->success();
        }
        return $this->fail('取消失败');
    }

}
